package org.kanstantin.nwscan.crawler;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Logger;

/**
 * @author kanstantin
 *
 * This is utility thread for observing what is going on while ThreadPoolExecutor has active threads
 */
public class MyMonitorThread implements Runnable {
   private final static Logger Log = Logger.getLogger(MyMonitorThread.class.getName());
    
   ThreadPoolExecutor executor;

   public MyMonitorThread(ThreadPoolExecutor executor)
   {
       this.executor = executor;
   }

   @Override
   public void run()
   {
       try
       {
           do
           {
               Log.info(
                       String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s, Queue size: %d",
                               this.executor.getPoolSize(),
                               this.executor.getCorePoolSize(),
                               this.executor.getActiveCount(),
                               this.executor.getCompletedTaskCount(),
                               this.executor.getTaskCount(),
                               this.executor.isShutdown(),
                               this.executor.isTerminated(),
                               this.executor.getQueue().size()));

               Thread.sleep(5000);
           }
           while (this.executor.getActiveCount() > 0);
       }
       catch (Exception e)
       {
       }
   }
}
